<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form">
            <div class="layui-form-item">
                <label class="layui-form-label required">管理账号</label>
                <div class="layui-input-block">
                    <input type="text" name="username" lay-verify="required|username" lay-reqtext="管理账号不能为空" placeholder="请输入管理账号"  value="" class="layui-input">
                    <tip>填写自己管理账号的名称。</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">手机</label>
                <div class="layui-input-block">
                    <input type="number" name="phone" lay-verify="required|phone" lay-reqtext="手机不能为空" placeholder="请输入手机"  value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">邮箱</label>
                <div class="layui-input-block">
                    <input type="email" name="email" lay-verify="required|email"  lay-reqtext="邮箱不能为空"  placeholder="请输入邮箱"  value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.7" charset="utf-8"></script>
<script src="../js/common.js?v=2.0.3" charset="utf-8"></script>
<script>
    layui.use(['form','miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab;

        // 自定义验证规则
        form.verify({
            // 验证用户名，且为必填项
            username: function(value, elem) {
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^_)|(__)|(_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线';
                }
                if (/^\d+$/.test(value)) {
                    return '用户名不能全为数字';
                }
            },
        
            // 验证手机号
            phone: function(value, elem) {
                if (!/^1[3-9]\d{9}$/.test(value)) {
                    return '请输入正确的手机号';
                }
            },
        
            // 验证邮箱
            email: function(value, elem) {
                if (!/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/.test(value)) {
                    return '请输入正确的邮箱';
                }
            },
        });
        

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let username = localStorage.getItem(userName);
            let new_username = data.field.username;
            let phone = data.field.phone;
            let email = data.field.email;

            if (new_username == username) {
                layer.msg('请输入新的管理账号');
                return false;
            }

            $.ajax({
                url: serverApiUrl + "/auth/update_user_info",
                type: "post",
                contentType: "application/json",
                headers: {'Authorization': 'Bearer ' + localStorage.getItem(tokenKey)},
                data: JSON.stringify({username: username, new_username:new_username, phone: phone, email: email}),
                success: function(response) {
                    layer.msg('修改成功', function () {
                        window.location = '../login.html';
                    });
                },
                error: function(xhr) {
                    var error = JSON.parse(xhr.responseText);

                    layer.msg('修改失败', function () {
                        
                    });
                }
            });
        });

    });
</script>
</body>
</html>